package com.wgts.wgts.mvp.presenter.imp;

import android.util.Log;

import com.wgts.wgts.BuildConfig;
import com.wgts.wgts.mvp.model.IStartPageModel;
import com.wgts.wgts.mvp.presenter.IStartPagePersenter;
import com.wgts.wgts.mvp.view.IStartPageView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.NetWorkConstant;
import com.wgts.wgts.netmodle.NetWorkFactory;
import com.wgts.wgts.netmodle.resp.AdvertisingEntity;
import com.wgts.wgts.rxjava.MVPResultSubscriber;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class StartPagePersenter implements IStartPagePersenter {

    private IStartPageModel model;

    private IStartPageView view;


    public StartPagePersenter(IStartPageModel model, IStartPageView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void jumpResult() {
        model.jumpResult(new MVPResultSubscriber<Boolean>() {
            @Override
            public void netx(Boolean aBoolean) {
                view.jumpResult(aBoolean);
            }
        });
    }

    @Override
    public void jumpTime() {
        model.jumpTime(new MVPResultSubscriber<String>() {
            @Override
            public void netx(String s) {
                view.jumpTime();
            }
        });
    }

    @Override
    public void removeTime() {
        model.removeTime();
    }

    @Override
    public void advertising() {
        model.advertising(new HttpResultSubscriber<AdvertisingEntity>() {
            @Override
            public void onSuccess(AdvertisingEntity advertisingEntity) {
                view.advertising(advertisingEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void jumpTimeSecond() {
        model.jumpTimeSecond(new MVPResultSubscriber<String>() {
            @Override
            public void netx(String s) {
                view.jumpTime();
            }
        });
    }

    @Override
    public void pingIP() {
        pingIp1(NetWorkConstant.HEAD_PING1);
    }


    public void pingIp1(String url1){
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url1)
                .head()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("", "");
                pingIp2(NetWorkConstant.HEAD_PING2);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    Log.d("PING1", "onResponse: " + response.body().string());
                    //NetWorkConstant.BASE_URL = BuildConfig.BASE_PING_IP1;
                    NetWorkFactory.setIndexForHost(0);
                    NetWorkConstant.BASE_AGREEMENT = BuildConfig.BASE_PING_AGREEMENT1;
                    view.pingResult();
                }else{
                    pingIp2(NetWorkConstant.HEAD_PING2);
                }
            }
        });
    }

    public void pingIp2(String  url2){
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url2)
                .head()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("", "");
                pingIp3(NetWorkConstant.HEAD_PING3);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    Log.d("PING1", "onResponse: " + response.body().string());
                    //NetWorkConstant.BASE_URL = BuildConfig.BASE_PING_IP2;
                    NetWorkFactory.setIndexForHost(1);
                    NetWorkConstant.BASE_AGREEMENT = BuildConfig.BASE_PING_AGREEMENT2;
                    view.pingResult();
                }else{
                    pingIp3(NetWorkConstant.HEAD_PING3);
                }
            }
        });
    }

    public void pingIp3(String url3){
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url3)
                .head()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                view.pingResult();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    Log.d("PING1", "onResponse: " + response.body().string());
                    //NetWorkConstant.BASE_URL = BuildConfig.BASE_PING_IP3;
                    NetWorkFactory.setIndexForHost(2);
                    NetWorkConstant.BASE_AGREEMENT = BuildConfig.BASE_PING_AGREEMENT3;
                    view.pingResult();
                }else{
                    pingIp4(NetWorkConstant.HEAD_PING4);
                }
            }
        });
    }

    public void pingIp4(String url4){
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url4)
                .head()
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                view.pingResult();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    Log.d("PING1", "onResponse: " + response.body().string());
                    //NetWorkConstant.BASE_URL = BuildConfig.BASE_PING_IP3;
                    NetWorkFactory.setIndexForHost(3);
                    NetWorkConstant.BASE_AGREEMENT = BuildConfig.BASE_PING_AGREEMENT3;
                    view.pingResult();
                }else{
                    Log.d("PING1", "onResponse: " + response.body().string());
                    //NetWorkConstant.BASE_URL = BuildConfig.BASE_PING_IP1;
                    NetWorkFactory.setIndexForHost(0);
                    NetWorkConstant.BASE_AGREEMENT = BuildConfig.BASE_PING_AGREEMENT1;
                    view.pingResult();
                }
            }
        });
    }





}
